package com.fattyCorps.battle.service;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.common.obj.msg.server.battle.CBattle;
import com.fattyCorps.common.obj.msg.server.battle.DHeroShort;
import com.fattyCorps.common.obj.msg.server.battle.SBattleResult;
import com.fattyCorps.battle.service.battle.Battle;
import com.fattyCorps.battle.service.battle.data.BattleData;
import com.fattyCorps.battle.service.battle.data.BattleHero;
import com.fattyCorps.battle.service.battle.data.Team;
import com.fattyCorps.common.mgr.Cfg;
import com.fattyCorps.common.obj.config.HeroMould;
import com.fattyCorps.common.packet.hero.Hero;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BattleService {
    public SBattleResult battleTest() {
        BattleData data = new BattleData();
        Battle battle = new Battle();

        Team left = new Team();
        left.getHeroes().add(makeBattleHero(battle, 1, true, 5, 1, 5));
        left.getHeroes().add(makeBattleHero(battle, 2, true, 4, 1, 5));
        left.getHeroes().add(makeBattleHero(battle, 3, true, 4, 1, 5));
        left.getHeroes().add(makeBattleHero(battle, 4, true, 4, 1, 5));
        left.getHeroes().add(makeBattleHero(battle, 5, true, 4, 1, 5));
        left.getHeroes().add(makeBattleHero(battle, 6, true, 5, 1, 5));

        Team right = new Team();
        right.getHeroes().add(makeBattleHero(battle, 1, false, 5, 1, 5));
        right.getHeroes().add(makeBattleHero(battle, 2, false, 4, 1, 5));
        right.getHeroes().add(makeBattleHero(battle, 3, false, 5, 1, 5));
        right.getHeroes().add(makeBattleHero(battle, 4, false, 5, 1, 5));
        right.getHeroes().add(makeBattleHero(battle, 5, false, 4, 1, 5));
        right.getHeroes().add(makeBattleHero(battle, 6, false, 4, 1, 5));

        data.setLeft(left);
        data.setRight(right);

        battle.init(data);
        long bTime = System.currentTimeMillis();
        battle.run();
        long eTime = System.currentTimeMillis();
        log.info("战斗耗时：" + (eTime - bTime));
        return battle.getResult();
    }

    private BattleHero makeBattleHero(Battle battle, int standPosNo, boolean leftTeam, int typeId, int lv, int star) {
        HeroMould heroMould = SpringUtil.getBean(Cfg.class).getCfg(HeroMould.class, typeId);
        Hero hero = Hero.makeHero(heroMould);
        hero.setLvAndUpdate(lv);
        hero.setStarAndUpdate(star);

        BattleHero battleHero = new BattleHero(hero, battle, standPosNo, leftTeam);

        return battleHero;
    }

    private Team makeTeam(Battle battle, boolean left, List<DHeroShort> heroes) {
        Team team = new Team();

        for (DHeroShort dHeroShort : heroes) {
            team.getHeroes().add(makeBattleHero(battle,
                    dHeroShort.getPosNo(), left, dHeroShort.getTypeId(),
                    dHeroShort.getLv(), dHeroShort.getStar()));
        }

        return team;
    }

    public SBattleResult battle(CBattle msg) {
        BattleData data = new BattleData();
        Battle battle = new Battle();

        Team left = makeTeam(battle, true, msg.getLeftTeam());
        Team right = makeTeam(battle, false, msg.getRightTeam());

        data.setLeft(left);
        data.setRight(right);

        battle.init(data);
        long bTime = System.currentTimeMillis();
        battle.run();
        long eTime = System.currentTimeMillis();
        log.info("战斗耗时：" + (eTime - bTime));
        return battle.getResult();
    }

}
